package cn.mumzone.pos.model.source;

import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.net.source.AbsRepository;
import cn.mumzone.basic.util.DeviceIdUtils;
import cn.mumzone.basic.util.RxUtil;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.GoodsTreeEntity;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.ReturnEntity;
import cn.mumzone.pos.model.ReturnGoodsEntity;
import okhttp3.RequestBody;
import retrofit2.Response;
import retrofit2.http.Query;

/**
 * Created by JoeLjt on 2019/6/27.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class GoodsRepository extends AbsRepository {


    public void searchGoodsList(String goodsIds, MemberEntity memberEntity, String scanCode, String pageNum, String pageSize, String isScan,
                                ApiCallBack<PageListResponse<GoodsEntity>> apiCallBack) {
        String memberId = null == memberEntity ? "" : memberEntity.getMemberId();
        String discountType = null == memberEntity ? "" : memberEntity.getDiscountType();
        String memberDiscount = null == memberEntity ? "" : memberEntity.getMemberDiscount();
        String memberPriceLevelId = null == memberEntity ? "" : memberEntity.getMemberPriceLevelId();

        ApiRequest.getGoodsService()
                .searchGoods(goodsIds, memberId, discountType, memberDiscount, memberPriceLevelId, scanCode, pageNum, pageSize, isScan)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<GoodsEntity>>>>applySchedulers())
                .subscribe(apiCallBack);
    }

    public void selectGoodsPrice(String goodsIds, MemberEntity memberEntity, ApiCallBack<PageListResponse<GoodsEntity>> apiCallBack) {
        String memberId = null == memberEntity ? "" : memberEntity.getMemberId();
        String discountType = null == memberEntity ? "" : memberEntity.getDiscountType();
        String memberDiscount = null == memberEntity ? "" : memberEntity.getMemberDiscount();
        String memberPriceLevelId = null == memberEntity ? "" : memberEntity.getMemberPriceLevelId();
        ApiRequest.getGoodsService()
                .selectGoodsPrice(goodsIds, memberId, discountType, memberDiscount, memberPriceLevelId)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<GoodsEntity>>>>applySchedulers())
                .subscribe(apiCallBack);
    }

    public void searchIntegralGoodsList(String keyword, String pageNum, String pageSize, ApiCallBack<PageListResponse<GoodsEntity>> apiCallBack) {
        ApiRequest.getGoodsService()
                .searchIntegralGoods(keyword, pageNum, pageSize)
                .compose(RxUtil.<Response<BaseResponse<PageListResponse<GoodsEntity>>>>applySchedulers())
                .subscribe(apiCallBack);
    }

    public void addGoodsToCart(RequestBody body, ApiCallBack<String> apiCallBack) {
        ApiRequest.getGoodsService()
                .addGoodsToCart(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(apiCallBack);
    }

    public void addGoodsToCartBatch(RequestBody body, ApiCallBack<String> apiCallBack) {
        ApiRequest.getGoodsService()
                .addGoodsToCartBatch(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(apiCallBack);
    }

    public void clearCartGoods(ApiCallBack<String> callBack) {
        ApiRequest.getGoodsService()
                .clearCartGoods(AppConstants.TEMP_SHOP_ID)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void removeGoodsById(String goodsId, String goodsSpec, ApiCallBack<String> callBack) {
        ApiRequest.getGoodsService()
                .removeGoodsById(AppConstants.TEMP_SHOP_ID, goodsId, goodsSpec)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);

    }

    public void searchGoodsReturnList(String orderCode, String goodsCode, ApiCallBack<List<ReturnGoodsEntity>> apiCallBack) {
        ApiRequest.getGoodsService()
                .searchGoodsForReturn(orderCode, goodsCode)
                .compose(RxUtil.<Response<BaseResponse<List<ReturnGoodsEntity>>>>applySchedulers())
                .subscribe(apiCallBack);
    }


    public void confirmForReturn(String goodsList, String orderCode, String returnAmount, String returnMargin, ApiCallBack<List<ReturnEntity>> callBack) {
        ApiRequest.getGoodsService()
                .confirmForReturn(goodsList, orderCode, returnAmount, returnMargin, DeviceIdUtils.getDeviceId())
                .compose(RxUtil.<Response<BaseResponse<List<ReturnEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getCartGoods(ApiCallBack<List<GoodsEntity>> apiCallBack) {
        ApiRequest.getGoodsService()
                .getCartGoods(AppConstants.TEMP_SHOP_ID)
                .compose(RxUtil.<Response<BaseResponse<List<GoodsEntity>>>>applySchedulers())
                .subscribe(apiCallBack);
    }

    public void refundPayCallBackToPhysicalStore(HashMap<String, String> settleJson, ApiCallBack<List<ReturnEntity>> callBack) {
        ApiRequest.getGoodsService()
                .refundPayCallBackToPhysicalStore(settleJson)
                .compose(RxUtil.<Response<BaseResponse<List<ReturnEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getGoodsClassifyTreeList(ApiCallBack<List<GoodsTreeEntity>> apiCallBack) {
        ApiRequest.getGoodsService()
                .getGoodsClassifyTreeList()
                .compose(RxUtil.<Response<BaseResponse<List<GoodsTreeEntity>>>>applySchedulers())
                .subscribe(apiCallBack);
    }

}
